"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DESC/DESCRIBE
Case Name   : desc查询ustore表结构
Create At   : 2022-10-09
Owner       : @zou_jialiang050
Description :
    1.设置guc参数
    2.创建兼容B库
    3.B库下建表
    4.desc分析表结构
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.返回表结构
    5.成功
History     : 
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class DESC(unittest.TestCase):
    def setUp(self):
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.database = 'd_compatibility_desc_0004'
        self.table = 't_compatibility_desc_0004'

    def test_desc(self):
        text1 = 'step1:设置guc参数 expect:成功'
        self.param = self.com.show_param('enable_default_ustore_table')
        guc_res = self.pri_sh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            'enable_default_ustore_table=on')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text1)

        text2 = 'step2:创建兼容B库 expect:成功'
        self.log.info(text2)
        create_database = self.pri_sh.execut_db_sql(
            f'''drop database if exists {self.database};
            create database {self.database} dbcompatibility='B';''')
        self.log.info(create_database)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_database,
                      '执行失败' + text2)

        text3 = 'step3:B库下建表 expect:成功'
        self.log.info(text3)
        create_table = self.pri_sh.execut_db_sql(
            f'''drop table if exists {self.table};
            create table {self.table}(c_int int default null,c_test 
            varchar(10) default ' ');''', dbname=self.database)
        self.log.info(create_table)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, create_table,
                      '执行失败' + text3)

        text4 = 'step4:desc分析表结构 expect:返回表结构'
        self.log.info(text4)
        desc_table = self.pri_sh.execut_db_sql(
            f'''desc {self.table};''', dbname=self.database)
        self.log.info(desc_table)
        self.assertIn('c_int  | integer               | YES  |     | NULL',
                      desc_table, '执行失败' + text4)
        self.assertIn("c_test | character varying(10) | YES  |     | ' ':"
                      ":character varying", desc_table, '执行失败' + text4)

    def tearDown(self):
        text5 = 'step5:清理环境 expect:成功'
        self.log.info(text5)
        clean_environment = self.pri_sh.execut_db_sql(
            f'''drop table {self.table};''', dbname=self.database)
        self.log.info(clean_environment)
        clean_environment1 = self.pri_sh.execut_db_sql(
            f'''drop database {self.database};''')
        self.log.info(clean_environment1)
        guc_res = self.pri_sh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_default_ustore_table={self.param}')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text5)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, clean_environment,
                      '执行失败' + text5)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_environment1,
                      '执行失败' + text5)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

